package com.oa.modules.approvalsec1.service.impl;

import cn.hutool.http.HttpRequest;
import com.google.gson.Gson;
import com.oa.common.utils.PageUtils;
import com.oa.modules.approvalsec1.service.AssistInManagingService;
import com.oa.modules.approvalsec1.service.ChatService;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Hankaicheng
 * @Date: 2022/3/1 14:51
 */
@Service
@Log4j2
public class ChatServiceImpl implements ChatService {


    @Value("${api.url}")
    private String URL;

    @Autowired
    private AssistInManagingService assistInManagingService;

    /**
     * 发送消息
     */
    @Override
    public Map sendMessage(Map<String, Object> params) {
        log.error(params);
        Gson gson = new Gson();
        String paramsJson = gson.toJson(params);
        String response = HttpRequest.post(URL + "/app/user/message")
                .body(paramsJson)
                .execute().body();
        log.error(response);
        Map result = gson.fromJson(response, Map.class);
        return result;
    }

    /**
     * 获取消息
     * @return
     */
    @Override
    public Map<String, Object> getMessage() {
        Gson gson = new Gson();
        String response = HttpRequest.post(URL + "/frontdesk/userInfo")
                .execute().body();
        Map result = gson.fromJson(response, Map.class);
        return result;
    }

    @Override
    public Map<String, Object> getQuestions(Map<String, Object> params) {
        Gson gson = new Gson();
        String body = HttpRequest.post(URL + "/api/im/qaList")
                .form(params)
                .execute().body();
        Map result = gson.fromJson(body, Map.class);
        return result;
    }

    @Override
    public Map<String, Object> uploadFile(File file) {
        Gson gson = new Gson();
        String body = HttpRequest.post(URL + "/api/im/uploadFile")
                .form("file", file)
                .execute().body();
        Map result = gson.fromJson(body, Map.class);
        return result;
    }

    @Override
    public Map<String, Object> evaluation(Map<String, Object> params) {
        Gson gson = new Gson();
        String s = gson.toJson(params);
        String token = assistInManagingService.getToken();
        String response = HttpRequest.post(URL + "/frontdesk/consult/comment")
                .header("x-auth-token", token)
                .header("Content-Type","application/x-www-form-urlencoded")
                .form(params)
                .execute().body();
        Map result = gson.fromJson(response, Map.class);
        return result;
    }


}
